/**
 * Created by Administrator on 2017/8/31 0031.
 */
var disUser = {};
var disUserDao = require('../dao/disUserDao');
var fs = require('fs');

disUser.getUserInfo = function (req,res) {

    var openId = req.headers.disopenid;
    if(!openId){
        res.send({code:1,info:'参数缺失'});
        return;
    }
    disUserDao.getUserById(openId,null,null,function (err,user) {
        if(err || !user){
            res.send({code:1,info:'获取用户信息失败!'});
        }else {
            res.send({code:0,user:user});
        }
    });
};

disUser.getUidInfo = function (req,res) {

    var uid = req.headers.disuid;
    if(!uid){
        res.send({code:1,info:'参数缺失'});
        return;
    }
    disUserDao.getUserByUid(uid,function (err,user) {
        if(err || !user){
            res.send({code:1,info:'获取用户信息失败!'});
        }else {
            res.send({code:0,user:user});
        }
    });
};

disUser.disPoinPay = function (req,res) {

    var openId = req.headers.disopenid;
    if(!openId){
        res.send({code:1,info:'参数缺失'});
        return;
    }
    disUserDao.getUserById(openId,null,null, function (err,user) {
        if(err || !user){
            res.send({code:1,info:'获取用户信息失败!'});
            return;
        }

        if(user.vip == 1){
            res.send({code:1,info:'已经是会员!'});
            return;
        }

        if(user.poin < 10){
            res.send({code:1,info:'积分不足!'});
            return;
        }

        disUserDao.increaseUserPoin(openId,-10, function (err) {
            if(err){
                res.send({code:1,info:'积分扣除失败,请重试!'});
            }else {
                disUserDao.setUserVip(openId,function (err) {      //设置为VIP
                    if (err) {
                        console.log('支付成功,但没有成为会员',openId);
                        res.send({code:1,info:'支付成功,请联系客服成为会员'});
                        return;
                    }

                    disUserDao.recordUserAction(user.uid,openId,1);

                    disUserDao.getUserById(openId,null,null, function (err,user) {
                        if(!err && user){
                            if(user.ref1 && user.ref1 != '0'){       //一级推荐人500分,5元
                                judgeComi(user.ref1,openId);
                            }
                            /*if(user.ref2 && user.ref2 != '0'){
                                disUserDao.addCommission(user.ref2,50,openId)
                            }
                            if(user.ref3 && user.ref3 != '0'){
                                disUserDao.addCommission(user.ref3,50,openId)
                            }
                            if(user.ref4 && user.ref4 != '0'){
                                disUserDao.addCommission(user.ref4,50,openId)
                            }
                            if(user.ref5 && user.ref5 != '0'){
                                disUserDao.addCommission(user.ref5,50,openId)
                            }
                            if(user.ref6 && user.ref6 != '0'){
                                disUserDao.addCommission(user.ref6,50,openId)
                            }*/
                        }
                    });

                    res.send({code:0});
                });

            }
        });

    });
};

var judgeComi = function (openId,contributor) {

    disUserDao.getUserById(openId,null,null, function (err,user) {
        if(!err && user){
            if(user.vip == 1) disUserDao.addCommission(openId,500,contributor);     //给一级代理发放5块

            if(user.ref1 && user.ref1 != '0'){
                judgeComi2(user.ref1,contributor);
            }

            if(user.ref2 && user.ref2 != '0'){
                judgeComi2(user.ref2,contributor);
            }

            if(user.ref3 && user.ref3 != '0'){
                judgeComi2(user.ref3,contributor);
            }

            if(user.ref4 && user.ref4 != '0'){
                judgeComi2(user.ref4,contributor);
            }

            if(user.ref5 && user.ref5 != '0'){
                judgeComi2(user.ref5,contributor);
            }

        }
    });
};

var judgeComi2 = function (openId,contributor) {
    disUserDao.getUserById(openId,null,null, function (err,user) {
        if(!err && user) {
            if (user.vip == 1) disUserDao.addCommission(openId, 50, contributor);  //其他级别代理发放5毛
        }
    });
};

/**
 * 用户推荐人个数(包括1和2级)
 * @param req
 * @param res
 */
disUser.getRefereeNum = function (req,res) {
    var disopenid = req.headers.disopenid;
    if(!disopenid){
        res.send({code:1,info:'参数缺失'});
        return;
    }
    disUserDao.getRefereeNum(disopenid,function (err,refNum) {
        if(err){
            res.send({code:1,info:'获取信息失败!'});
        }else {
            res.send({code:0,refNum:refNum});
        }
    });
};

/**
 * 用户推荐人个数(包括1和2级)
 * @param req
 * @param res
 */
disUser.getDisRefereeInfo = function (req,res) {
    var disOpenid = req.headers.disopenid;
    var ref = parseInt(req.headers.ref);      //有值为获取直推人数

    if(!disOpenid){
        res.send({code:1,info:'参数缺失'});
        return;
    }

    if(ref){    //获取直推人信息
        disUserDao.getRef1info(disOpenid, function (err,refInfo) {
            if(err){
                res.send({code:1,info:'获取信息失败!'});
            }else {
                res.send({code:0,refInfo:refInfo});
            }
        });
    }else {
        disUserDao.getRef2info(disOpenid, function (err,refInfo) {
            if(err){
                res.send({code:1,info:'获取信息失败!'});
            }else {
                res.send({code:0,refInfo:refInfo});
            }
        });
    }

};

/**
 * 提现记录
 * @param req
 * @param res
 */
disUser.getCashList = function (req,res) {
    var disOpenid = req.headers.disopenid;
    if(!disOpenid){
        res.send({code:1,info:'参数缺失'});
        return;
    }
    disUserDao.getCashList(disOpenid,function (err,list) {
        if(err){
            res.send({code:1,info:'获取信息失败!'});
        }else {
            res.send({code:0,list:list});
        }
    });
};

/**
 * 佣金记录
 * @param req
 * @param res
 */
disUser.getComiList = function (req,res) {
    var disOpenid = req.headers.disopenid;
    if(!disOpenid){
        res.send({code:1,info:'参数缺失'});
        return;
    }
    disUserDao.getComiList(disOpenid,function (err,list) {
        if(err){
            res.send({code:1,info:'获取信息失败!'});
        }else {
            res.send({code:0,list:list});
        }
    });
};

/**
 * 积分记录
 * @param req
 * @param res
 */
disUser.getPoinList = function (req,res) {
    var disUid = req.headers.disuid;
    if(!disUid){
        res.send({code:1,info:'参数缺失'});
        return;
    }
    disUserDao.getPoinList(disUid,function (err,list) {
        if(err){
            res.send({code:1,info:'获取信息失败!'});
        }else {
            res.send({code:0,list:list});
        }
    });
};

/**
 * 提取未提现佣金
 * @param req
 * @param res
 */
disUser.extractCoin = function (req,res) {
    var disOpenid = req.headers.disopenid;
    if(!disOpenid){
        res.send({code:1,info:'参数缺失'});
        return;
    }
    disUserDao.extractCoin(disOpenid,function (err,totalCoin) {
        if(err){
            res.send({code:1,info:'提取佣金失败,请重试'});
        }else {
            if(totalCoin){
                disUserDao.increaseUserCoin(disOpenid,totalCoin, function () {});
            }
            res.send({code:0,totalCoin:totalCoin});
        }
    });
};

disUser.sendPoin = function (req,res) {
    var disOpenid = req.headers.disopenid;
    var disUid = req.headers.disuid;
    var tarUid = req.body.tarUid;
    var sendPoin = req.body.sendPoin;

    if(!disOpenid || !disUid || !tarUid || !sendPoin){
        res.send({code:1,info:'参数缺失!'});
        return;
    }

    if(disUid == tarUid){
        res.send({code:1,info:'不能送给自己!'});
        return;
    }

    disUserDao.getUserById(disOpenid,null,null, function (err,selfInfo) {
        if(err || !selfInfo){
            res.send({code:1,info:'玩家信息错误!'});
            return;
        }

        if(selfInfo.role == 1){
            res.send({code:1,info:'您不允许赠送积分!'});
            return;
        }

        if(sendPoin > selfInfo.poin){
            res.send({code:1,info:'积分不足!'});
            return;
        }

        disUserDao.getUserByUid(tarUid, function (err,targetInfo) {
            if(err || !targetInfo){
                res.send({code:1,info:'赠送对象不存在!'});
                return;
            }

            if(targetInfo.role == 1){
                res.send({code:1,info:'不允许赠送给改用户!'});
                return;
            }

            disUserDao.increaseUserPoinByUid(disUid,-sendPoin, function (err) {     //先扣积分
                if(err){
                    res.send({code:1,info:'赠送失败!'});
                    return;
                }else {
                    disUserDao.increaseUserPoinByUid(tarUid,sendPoin, function (err) {  //目标加上积分
                        if(err){
                            res.send({code:1,info:'赠送失败!'});
                            disUserDao.increaseUserPoinByUid(disUid,sendPoin, function () {});      //失败给原主加回积分
                            return;
                        }
                        disUserDao.recordUserPoin(disUid,sendPoin,1,tarUid, function () {});
                        disUserDao.recordUserPoin(tarUid,sendPoin,2,disUid, function () {});

                        res.send({code:0})
                    });
                }
            })

        });

    });

};

/**
 * 管理员后台
 */

disUser.getDisUser = function (req,res) {
    var disOpenid = req.headers.disopenid;
    if(!disOpenid || (disOpenid != 'o4AyEt_EpmY83VE17bCOJ-t8MIEg' && disOpenid != 'o4AyEt2gypi_z981XBQzM5in0q_U')){
        res.send({code:1,info:'没有权限!'});
        return;
    }
    disUserDao.getDisUser(function (err,users) {
        if(err){
            res.send({code:1,info:'获取数据失败,请重试'});
        }else {
            res.send({code:0,users:users});
        }
    });
};

disUser.getDisOrders = function (req,res) {
    var disOpenid = req.headers.disopenid;
    if(!disOpenid || (disOpenid != 'o4AyEt_EpmY83VE17bCOJ-t8MIEg' && disOpenid != 'o4AyEt2gypi_z981XBQzM5in0q_U')){
        res.send({code:1,info:'没有权限!'});
        return;
    }
    disUserDao.getDisOrders(function (err,orders) {
        if(err){
            res.send({code:1,info:'获取数据失败,请重试'});
        }else {
            res.send({code:0,orders:orders});
        }
    });
};
disUser.getDisCash = function (req,res) {
    var disOpenid = req.headers.disopenid;
    if(!disOpenid || (disOpenid != 'o4AyEt_EpmY83VE17bCOJ-t8MIEg' && disOpenid != 'o4AyEt2gypi_z981XBQzM5in0q_U')){
        res.send({code:1,info:'没有权限!'});
        return;
    }
    disUserDao.getDisCash(function (err,lists) {
        if(err){
            res.send({code:1,info:'获取数据失败,请重试'});
        }else {
            res.send({code:0,lists:lists});
        }
    });
};

/************渠道管理****************/

disUser.getDisChannelWorth = function (req,res) {
    var channel = req.headers.channel;
    if(!channel){
        res.send({code:1,info:'没有权限!'});
        return;
    }
    disUserDao.getDisChannelWorth(channel, function (err,data) {
        if(err){
            res.send({code:1,info:'参数错误!'});
            return;
        }
        res.send({code:0,data:data});
    })
};

disUser.balanceDisChannel = function (req,res) {
    var channel = req.headers.channel;
    var openid = req.headers.openid;

    if(!channel || openid != 'o4AyEt_EpmY83VE17bCOJ-t8MIEg' && openid != 'o4AyEt2gypi_z981XBQzM5in0q_U'){
        res.send({code:1,info:'没有权限!'});
        return;
    }

    disUserDao.balanceDisChannel(channel,openid, function (err,data) {
        if(err){
            res.send({code:1,info:'参数错误!'});
            return;
        }
        res.send({code:0,data:data});
    })
};


disUser.listDisChannel = function (req,res) {
    var channel = req.headers.channel;
    var openid = req.headers.openid;
    var body = req.body;
    //console.log('body:%s openid:%s',JSON.stringify(body),openid);
    var coinValue = body.coinValue,orderNum = body.orderNum,cashNum = body.cashNum,dividedRmb = body.dividedRmb;
    if(!channel || openid != 'o4AyEt_EpmY83VE17bCOJ-t8MIEg' && openid != 'o4AyEt2gypi_z981XBQzM5in0q_U'){
        res.send({code:1,info:'没有权限!'});
        return;
    }

    disUserDao.listDisChannel(openid,channel,coinValue,orderNum,cashNum,dividedRmb, function (err,data) {
        if(err){
            res.send({code:1,info:'参数错误!'});
            return;
        }
        //disUserDao.balanceDisChannel(channel,openid, function () {});
        res.send({code:0,data:data});
    })
};

disUser.listDisHistory = function (req,res) {
    var channel = req.headers.channel;

    if(!channel){
        res.send({code:1,info:'没有权限!'});
        return;
    }

    disUserDao.channelDisHistory(channel, function (err,data) {
        if(err){
            res.send({code:1,info:'参数错误!'});
            return;
        }
        res.send({code:0,data:data});
    });
};

disUser.getDisChannelUser = function (req,res) {
    var channel = req.headers.channel;
    if(!channel){
        res.send({code:1,info:'没有权限!'});
        return;
    }
    disUserDao.getDisChannelUser(channel, function (err,lists) {
        if(err){
            res.send({code:1,info:'参数错误!'});
            return;
        }
        res.send({code:0,lists:lists});
    })
};

disUser.getDisChannelOrders = function (req,res) {
    var channel = req.headers.channel;
    if(!channel){
        res.send({code:1,info:'没有权限!'});
        return;
    }
    disUserDao.getDisChannelOrders(channel, function (err,lists) {
        if(err){
            res.send({code:1,info:'参数错误!'});
            return;
        }
        res.send({code:0,lists:lists});
    })
};

disUser.getDisChannelCash = function (req,res) {
    var channel = req.headers.channel;
    if(!channel){
        res.send({code:1,info:'没有权限!'});
        return;
    }
    disUserDao.getDisChannelCash(channel, function (err,lists) {
        if(err){
            res.send({code:1,info:'参数错误!'});
            return;
        }
        res.send({code:0,lists:lists});

    })
};

/************工具函数****************/

//批量插入
disUser.batchCreate = function () {
    var ref1 = 'o4AyEt2gypi_z981XBQzM5in0q_U';
    var timer = setInterval(function () {
        var oid = createNonceStr(5);
        disUserDao.createUser(oid,ref1, function (err) {
            if(err){
                console.log('batchCreate err!');
            }

        })
    },500);

    setTimeout(function () {
        clearInterval(timer);
    },50000);

};


//批量插入
disUser.createCash = function () {
    var ref2 = 'o4AyEt2gypi_z981XBQzM5in0q_U';

    var timer = setInterval(function () {
        var oid = createNonceStr(20);
        var uid = Math.floor(Math.random() * 48)+10200;
        var cash = Math.floor(Math.random() * 500 + 100);
        disUserDao.createCash(uid,ref2,cash,oid, function (err) {
            if(err){
                console.log('createCash err!');
            }

        })
    },800);

    setTimeout(function () {
        clearInterval(timer);
    },80000);

};

disUser.createOrders = function () {
    var ref2 = 'o4AyEt2gypi_z981XBQzM5in0q_U';

    var timer = setInterval(function () {
        var oid = createNonceStr(20);
        var uid = Math.floor(Math.random() * 48)+10200;
        //var cash = Math.floor(Math.random() * 1000);
        disUserDao.createOrders(uid,ref2,1000,oid,oid, function (err) {
            if(err){
                console.log('createCash err!');
            }

        })
    },800);

    setTimeout(function () {
        clearInterval(timer);
    },80000);

};



disUser.createCom = function () {
    var openId = 'o4AyEt_EpmY83VE17bCOJ-t8MIEg';

    var timer = setInterval(function () {
        var coin = 500;
        var oid = createNonceStr(20);
        if(Math.random() < 0.5) coin = 50;
        disUserDao.createCom(10210,openId,coin,oid, function (err) {
            if(err){
                console.log('createCommission err!');
            }

        })
    },800);

    setTimeout(function () {
        clearInterval(timer);
    },8000);

};

disUser.createPoin = function () {

    var timer = setInterval(function () {
        var poin =parseInt( Math.random() * 10);
        var action =Math.floor( Math.random() * 2 +1);
        var target =parseInt( Math.random() * 1000+10000);
        disUserDao.createPoin(10200,poin,action,target, function (err) {
            if(err){
                console.log('createPoin err!');
            }
        })
    },800);

    setTimeout(function () {
        clearInterval(timer);
    },80000);

};

function createNonceStr(len) {
    return Math.random().toString(36).substr(2, len);
};

module.exports = disUser;






